package com.mlming.springboot.controller;

import com.mlming.springboot.consts.MallConst;
import com.mlming.springboot.form.CartAddForm;
import com.mlming.springboot.form.CartUpdateForm;
import com.mlming.springboot.pojo.User;
import com.mlming.springboot.service.CartService;
import com.mlming.springboot.util.JWTUtil;
import com.mlming.springboot.vo.CartVo;
import com.mlming.springboot.vo.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Map;

@RestController
public class CartController {
    @Autowired
    private CartService cartService;

    @GetMapping("/carts")
    public ResponseVo<CartVo> getCarts(HttpServletRequest request) {
        // User user = (User) httpSession.getAttribute(MallConst.CURRENT_USER);
        String token = request.getHeader(MallConst.TOKEN_NAME);
        Integer userId = Integer.valueOf(JWTUtil.getFieldValuePayLoad(token, MallConst.USER_ID_IN_PAYLOAD));
        return cartService.getCartList(userId);
    }

    @PostMapping("/carts")
    public ResponseVo<CartVo> addCart(@Valid CartAddForm cartAddForm,
                                      HttpServletRequest request) {
        String token = request.getHeader(MallConst.TOKEN_NAME);
        Integer userId = Integer.valueOf(JWTUtil.getFieldValuePayLoad(token, MallConst.USER_ID_IN_PAYLOAD));
        return cartService.addCartItem(userId,cartAddForm);
    }

    @PutMapping("/carts/{productId}")
    public ResponseVo<CartVo> updateCart(@PathVariable("productId") Integer productId,
                                      CartUpdateForm cartUpdateForm,
                                      HttpServletRequest request) {
        String token = request.getHeader(MallConst.TOKEN_NAME);
        Integer userId = Integer.valueOf(JWTUtil.getFieldValuePayLoad(token, MallConst.USER_ID_IN_PAYLOAD));
        return cartService.updateCartItem(userId,productId,cartUpdateForm);
    }

    @DeleteMapping("/carts/{productId}")
    public ResponseVo<CartVo> deleteCart(@PathVariable("productId") Integer productId,
                                      HttpServletRequest request) {
        String token = request.getHeader(MallConst.TOKEN_NAME);
        Integer userId = Integer.valueOf(JWTUtil.getFieldValuePayLoad(token, MallConst.USER_ID_IN_PAYLOAD));
        return cartService.deleteCartItem(userId,productId);
    }

    @PutMapping("/carts/selectAll")
    public ResponseVo<CartVo> selectAll(HttpServletRequest request) {
        String token = request.getHeader(MallConst.TOKEN_NAME);
        Integer userId = Integer.valueOf(JWTUtil.getFieldValuePayLoad(token, MallConst.USER_ID_IN_PAYLOAD));
        return cartService.selectAll(userId);
    }

    @PutMapping("/carts/unSelectAll")
    public ResponseVo<CartVo> unSelectAll(HttpServletRequest request) {
        String token = request.getHeader(MallConst.TOKEN_NAME);
        Integer userId = Integer.valueOf(JWTUtil.getFieldValuePayLoad(token, MallConst.USER_ID_IN_PAYLOAD));
        return cartService.unSelectAll(userId);
    }

    @GetMapping("/carts/products/sum")
    public ResponseVo<Integer> getCartCount(HttpServletRequest request) {
        String token = request.getHeader(MallConst.TOKEN_NAME);
        Integer userId = Integer.valueOf(JWTUtil.getFieldValuePayLoad(token, MallConst.USER_ID_IN_PAYLOAD));
        return cartService.sum(userId);
    }
}
